/*******************************************************************************

      ---------========= DSx86 Configuration Editor =========---------
                               by RyouArashi
                        http://ryouarashi.blogspot.com


              ---------========= License Agreement =========---------

Copyright 2010 RyouArashi. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY RyouArashi ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL RyouArashi OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

/*******************************************************************************
 * Profile Data Structures
 ******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "PROFILES.H"

struct ProfileList * ProfileList_Create()
{
  /* Create New Profile List */
  struct ProfileList * List;

  /* Alloc */
  List = (struct ProfileList *)malloc(sizeof(struct ProfileList));

  /* Init List if sucessfully alloced */
  if (List)
  {
    /* Set all Members to Zero */
    memset(List, 0, sizeof(struct ProfileList));
  }

  /* Return */
  return List;
}

void ProfileList_Free(struct ProfileList * Instance)
{
  /* Free List including all Profiles */

  if (Instance)
  {
    unsigned j;

    /* Free Records */
    for (j = 0; j < Instance->Capacity; j++)
    {
       if (Instance->Items[j]) free(Instance->Items[j]);
    }

    /* Free List Array */
    if (Instance->Items) free(Instance->Items);

    /* Free List */
    free(Instance);
  }
}

void ProfileList_Remove(struct ProfileList * Instance, unsigned Index)
{
  /* Remove Profile at Index */

  /* Check Index */
  if (Index >= Instance->Count)
  {
    return;
  }

  /* Remove Item */
  if (Instance->Items[Index]) free(Instance->Items[Index]);
  Instance->Count--;

  /* Move Items */
  while (Index < Instance->Count)
  {
    Instance->Items[Index] = Instance->Items[Index+1];
    Index++;
  }
  Instance->Items[Index+1] = NULL;
}

struct Profile * ProfileList_Append(struct ProfileList * Instance)
{
  /* Append Profile to List */

  /* Enlarge array if necessary */
  if (Instance->Count == Instance->Capacity)
  {
    struct Profile ** New;
    unsigned k;

    /* Increase Capacity */
    Instance->Capacity += 4;

    /* Realloc array */
    New = (struct Profile **)realloc(Instance->Items, Instance->Capacity *
      sizeof(struct Profile *));

    /* Check Result */
    if (!New)
    {
      return NULL;
    }

    /* Assign new array */
    Instance->Items = New;

    /* Clear new elements */
    for (k = Instance->Count; k < Instance->Capacity; k++)
    {
      New[k] = NULL;
    }
  }

  /* Create new Profile in List*/
  {
    struct Profile * New;

    /* Alloc Memory */
    New = (struct Profile *)malloc(sizeof(struct Profile));

    /* Check Result */
    if (!New)
    {
      return NULL;
    }

    /* Init Section */
    memset(New, 0, sizeof(struct Profile));

    /* Insert Section */
    Instance->Items[Instance->Count++] = New;

    /* Return */
    return New;
  }
}

struct Profile * ProfileList_Find(struct ProfileList * Instance, char * Name)
{
  unsigned j;

  /* Loop */
  for (j = 0; j < Instance->Count; j++)
  {
    struct Profile * P;

    /* Get Profile */
    P = Instance->Items[j];

    /* Compare */
    if (strcmp(Name, P->Name) == 0)
    {
      return P;
    }
  }

  /* Not found */
  return NULL;
}

/* End of File ****************************************************************/